package com.time.workshop.ui;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.time.workshop.API;
import com.time.workshop.BaseConstant;
import com.time.workshop.R;
import com.time.workshop.SuperActivity;
import com.time.workshop.injector.Injector;
import com.time.workshop.injector.V;
import com.time.workshop.utils.CommonUtils;
import com.time.workshop.utils.NewToast;
import com.time.workshop.utils.PreferenceUtil;

/**
 * @author fanbaolong
 *  密码修改
 */
public class ModifyPasswordActivity extends SuperActivity{

	@V
	private Button mod_pwd_titlebar_left_btn,mod_sure_but; 
	@V
	private EditText old_pwd_edit,new_pwd_edit,new_pwd_too_edit;

	@Override
	protected void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		setContentView(R.layout.activity_modify_pwd);

		init();

	}

	private void init(){
		Injector.getInstance().inject(this);

		mod_pwd_titlebar_left_btn.setOnClickListener(this);
		mod_sure_but.setOnClickListener(this);

	}

	public void onClick(View v){
		switch (v.getId()) {
		case R.id.mod_pwd_titlebar_left_btn:
			back();
			break;
		case R.id.mod_sure_but:
			doJudge();
			break;

		default:
			break;
		}

	}

	private void doJudge() {
//		判断
		if(invalidateText(old_pwd_edit,R.string.please_info_old_pwd) ||
				invalidateText(new_pwd_edit,R.string.please_info_new_pwd) ||
				invalidateText(new_pwd_too_edit,R.string.please_info_new_pwd_too)
				){
			return;
		}
		if ( ! getText(new_pwd_edit).equals(getText(new_pwd_too_edit))) {
//			App.showShortToast(R.string.new_pwd_is_not_the_same);
			NewToast.makeText(getApplicationContext(), R.string.new_pwd_is_not_the_same);
			return;
		}
		if(null == BaseConstant.PREF_TOKE_STRING  ||"".equals(BaseConstant.PREF_TOKE_STRING) ){
			NewToast.makeText(getApplicationContext(), R.string.please_login_first);
			return;
		}
		if (!CommonUtils.isNetworkConnected(getApplicationContext())) {
			NewToast.makeText(getApplicationContext(), getResources().getString(R.string.M000));
			return;
		}
		//请求
		doRequest();

	}

	private void doRequest() {
		showDialog(null);
		
		RequestParams params = new RequestParams();
		params.addQueryStringParameter("token", PreferenceUtil.getString(this, BaseConstant.PREF_TOKE_STRING));
		params.addQueryStringParameter("old_password", getText(old_pwd_edit));
		params.addQueryStringParameter("new_password", getText(new_pwd_edit));
		params.addQueryStringParameter("re_password", getText(new_pwd_too_edit));
		
		HttpUtils httpUtils = new HttpUtils();
		
		httpUtils.send(HttpMethod.GET, API.modfiyPwd, params, new RequestCallBack<String>() {

			@Override
			public void onSuccess(ResponseInfo<String> responseInfo) {
				dismissLoadingDialog();
				Log.d("onSuccess==", responseInfo.result);
				try {
					JSONObject jsonObject = new JSONObject(responseInfo.result);
					boolean success  = jsonObject.getBoolean("success");
					if(success ){
						NewToast.makeText(getApplicationContext(), R.string.pwd_mogify_success);
						back();
					}else {
						NewToast.makeText(getApplicationContext(), R.string.pwd_mogify_failure);
					}
					
					
				} catch (JSONException e) {
					e.printStackTrace();
				}
				
			}

			@Override
			public void onFailure(HttpException error, String msg) {
				dismissLoadingDialog();
				Log.d("onFailure==", msg);
				NewToast.makeText(getApplicationContext(), msg);
			}
			
			
		});
		
		
		
		
		
		
		
		
	}

}
